package org.leviatan.dataharbour.core.admin.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
import java.util.Vector;

import org.leviatan.dataharbour.core.admin.DataHarbourAdminService;
import org.leviatan.dataharbour.core.model.DataSource;
import org.leviatan.dataharbour.core.model.HarbourRequest;
import org.leviatan.dataharbour.core.model.PersistenceResource;
import org.leviatan.dataharbour.core.model.control.DataSourceHandler;
import org.leviatan.dataharbour.core.model.control.PersistenceResourceHandler;
import org.leviatan.dataharbour.core.model.impl.HibernatePersistenceResourceImpl;
import org.leviatan.definator.core.model.Table;

public class DataHarbourAdminServiceImpl implements DataHarbourAdminService {
	
	private static DataHarbourAdminServiceImpl instance;
	
	public static DataHarbourAdminService getInstance() {
		if (DataHarbourAdminServiceImpl.instance == null) {
			DataHarbourAdminServiceImpl.instance = new DataHarbourAdminServiceImpl();
		}
		return DataHarbourAdminServiceImpl.instance;
	}

	// Unpersisted harbour requests
	///////////////////////////////////////////////////////////////////////////
	public HashMap getAllUnpersistedHarbourRequests() {
		return null;
	}

	public Collection getSendersUnpersistedHarbourRequests(String senderId) {
		return null; //RegisterHandler.getUnpersistedHarbourRequests(senderId);
	}
	
	public HarbourRequest getUnpersistedHarbourRequest(String reqId) {
		return null; //RegisterHandler.getUnpersistedHarbourRequest(reqId);
	}
	
	public void removeUnpersistedHarbourRequest(String reqId) {
		//RegisterHandler.removeUnpersistedHarbourRequest(reqId);
	}

	// Register + configuration
	///////////////////////////////////////////////////////////////////////////
	public void loadConfiguration(String filePath) {
		// TODO Auto-generated method stub
	}

	public void loadModels() {
		RegisterHandler.loadDataSourcesFromFile();
		RegisterHandler.loadPersistenceResourcesFromFile();
		RegisterHandler.loadSendersFromFile();
		//RegisterHandler.loadHarbourRequestsFromFile();
	}

	public void loadRegisterMappings() {
		RegisterHandler.loadDataEntity2DefinatorFieldsFromFile();
		RegisterHandler.loadDataSource2DefinatorTablesFromFile();
		RegisterHandler.loadDataSource2PersistenceResourcesFromFile();
		RegisterHandler.loadHarbourRequest2DataEntities();
		RegisterHandler.loadSender2DataSourcesFromFile();
		RegisterHandler.loadSender2HarbourRequestsFromFile();
	}
	
	public void loadRegisterIds() {
		RegisterHandler.loadDataEntityIdList();
		RegisterHandler.loadHarbourRequestIdList();
		RegisterHandler.loadUnpersistedHarbourRequestIdList();
	}

	public void saveModels() {
		RegisterHandler.saveAllModelsOnFile();
	}

	public void saveRegister() {
		//TODO RegisterHandler.
	}

	public Map getAllDataSources() {
		return null;
	}

	public DataSource getDataSource(String id) {
		return null;
	}

	public void removeDataSource(String id) {
		//RegisterHandler.removeDataSourceModel(id);
		
	}

	public Vector<Table> getDataSourcesDefs(String dataSourceId) {
		TreeSet defKeys = RegisterHandler.getDataSourceId2DefinatorTableIds(dataSourceId);
		if (defKeys == null || defKeys.isEmpty())
			return null;
		Vector<Table> result = new Vector<Table>();
		String tblId;
		
		for (Iterator i = defKeys.iterator(); i.hasNext(); ) {
			tblId = i.next().toString();
			
			Table t = DefinatorHandler.getTableModel(tblId);
			if (t != null) result.add(t);
		}
		return result;
	}

	public TreeSet<String> getDataSourcesPersistenceResourceKeys(String dataSourceId) {
		return RegisterHandler.getDataSourceId2PersistenceResourceIds(dataSourceId);
	}
	
	public TreeSet<String> getDataSourcesPersistenceResources(String dsId) {
		return RegisterHandler.getDataSourceId2PersistenceResourceIds(dsId);
	}
	
	public void removeDefFromDataSource(String dataSourceId, String defId) {
		RegisterHandler.removeDataSource2DefinatorTableMapping(dataSourceId, defId);
	}

	public void removePersistenceResourceFromDataSource(String dataSourceId, String persResId) {
		RegisterHandler.removeDataSource2PersistenceResourceMapping(dataSourceId, persResId);
	}

	public void addDefToDataSource(String dsId, String defId) {
		RegisterHandler.addDataSource2DefinatorTableMapping(dsId, defId);	
	}

	public void addPersistenceResourceToDataSource(String dsId, String perResId) {
		RegisterHandler.addDataSource2PersistenceResourceMapping(dsId, perResId);
	}
	
	public Collection getAllVirtualStructures() {
		return DefinatorHandler.getAllDefs();	
	}

	public void saveDataSource2DefinitionsMappings() {
		RegisterHandler.storeDataSource2DefinatorTablesOnFile();
	}

	public void saveDataSource2PersistenceResourceMappings() {
		RegisterHandler.storeDataSource2PersistenceResourcesOnFile();
	}

	public HashMap getAllPersistenceResources() {
		return RegisterHandler.getAllPersistenceResourceModels();
	}

	public String getNewUniqueId() {
		return org.leviatan.common.keyutils.KeyGenerator.generateUniqueKeyAsString();
	}

	public String addDataSource(DataSource ds) {
		DataSourceHandler.setNewUniqueIdForDataSource(ds);
		RegisterHandler.addDataSource(ds);
		return ds.getId();
	}

	public String createDataSource(String name) {
		DataSource ds = DataSourceHandler.createNewDataSource(name);
		RegisterHandler.addDataSource(ds);
		return ds.getId();
	}

	// saves
	///////////////////////////////////////////////////////////////////////////
	public void saveDataSources() {
		RegisterHandler.storeDataSourceModelsOnFile();		
	}

	public void savePersistenceResources() {
		RegisterHandler.storePersistenceResourceModelsOnFile();
	}

	public void saveUnpersistedHarbourRequests() {
		RegisterHandler.storeUnpersistedHarbourRequestIdsOnFile();
	}
	
	///////////////////////////////////////////////////////////////////////////

	public PersistenceResource getPersistenceResource(String persResId) {
		return RegisterHandler.getPersistenceResourceModel(persResId);
	}

	public void removePersistenceResource(String persResId) {
		RegisterHandler.removePersistenceResource(persResId);
		
	}

	public void updateHibernatePersistenceResource(String id, String name, String hibConf) {
		PersistenceResource p = RegisterHandler.getPersistenceResourceModel(id);
		p.setName(name);
		if (p instanceof HibernatePersistenceResourceImpl) {
			HibernatePersistenceResourceImpl hpr = (HibernatePersistenceResourceImpl) p;
			hpr.setConfString(hibConf);
		}
	}

	public void updatePersistenceResource(String id, String name, String hibConf) {
		PersistenceResource p = RegisterHandler.getPersistenceResourceModel(id);
		p.setName(name);
	}

	public String createPersistenceResource(String name, String type) {
		PersistenceResource p =
			PersistenceResourceHandler.createPersistenceResource(name, type);
		
		RegisterHandler.addPersistenceResource(p);
		return p.getId();
	}

	public String createHibernatePersistenceResource(String name, String confString) {
		PersistenceResource p =
			PersistenceResourceHandler.createHibernatePersistenceResource(name, confString);
		RegisterHandler.addPersistenceResource(p);
		
		return p.getId();
	}

	public void updatePersistenceResource(String id, String name) {
		PersistenceResource p = RegisterHandler.getPersistenceResourceModel(id);
		p.setName(name);
	}

	public void updateDataSource(String id, String name) {
		DataSource d = RegisterHandler.getDataSourceModel(id);
		d.setName(name);
	}


}
